package com.bai.ruiji.Controller;

import com.bai.ruiji.Common.BaseContext;
import com.bai.ruiji.Common.R;
import com.bai.ruiji.Service.ShoppingService;
import com.bai.ruiji.Service.impl.ShoppingCartServiceImpl;
import com.bai.ruiji.pojo.ShoppingCart;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 27471
 * Time : 2022/10/15 11:29
 */
@RequestMapping("/shoppingCart")
@RestController//返回JSON数据
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingService shoppingService;

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info(shoppingCart.toString());
        //获取用户id
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        //判断是套餐还是菜品
        Long dishId = shoppingCart.getDishId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);
        if (dishId!=null){
            //添加为菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            //添加为套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingService.getOne(queryWrapper);
        //判断添加的菜品在表单里有没有
        if (one!=null) {
            //  有则更改number+1
            Integer number = one.getNumber();
            one.setNumber(number+1);
            shoppingService.updateById(one);
        }else {
            //  无则添加到购物车，number为1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingService.save(shoppingCart);
            one=shoppingCart;
        }
        return R.success(one);
    }

    /**
     * 查看购物车
     * @return
     */
    @RequestMapping("/list")
    public R<List<ShoppingCart>> list(){
        //获取用户id
        Long currentId = BaseContext.getCurrentId();

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingService.list(queryWrapper);
        return R.success(list);
    }

    @PostMapping("/sub")
    public R<ShoppingCart> delete(@RequestBody ShoppingCart shoppingCart){
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        log.info(shoppingCart.toString());
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,shoppingCart.getUserId());
        if (shoppingCart.getDishId()!=null){
             //菜品
           queryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
         }else {
             //套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
         }
        ShoppingCart one = shoppingService.getOne(queryWrapper);
        Integer name = one.getNumber();
        one.setNumber(name-1);
        shoppingService.updateById(one);

        return R.success(one);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    private R<String> delete(){
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);
        shoppingService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }
}
